﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General;
using Gtk;

#endregion


namespace ESF.GTK
{
	public static class GTKHelper
	{
		public static void SetDefaultUnhanledExceptionManagers ()
		{
			GLib.ExceptionManager.UnhandledException += 
				delegate (GLib.UnhandledExceptionArgs args)
				{
					Log.Error (args.ExceptionObject as Exception);
				};
		}


		#region Messages

		public static void ShowError (Window pWindow, string pValue)
		{
			Application.Invoke(delegate 
			{ 
				MessageDialog md = new MessageDialog(pWindow,
					  DialogFlags.Modal, MessageType.Error,
					  ButtonsType.Close, pValue);
				md.Run();
				md.Destroy();
			});
		}


		public static void ShowInformation (Window pWindow, string pValue)
		{
			Application.Invoke(delegate 
			{			
				MessageDialog md = new MessageDialog(pWindow,
					  DialogFlags.Modal, MessageType.Info,
					  ButtonsType.Ok, pValue);
				md.Run();
				md.Destroy();
			});
		}


		public static void ShowWarning(Window pWindow, string pValue)
		{
			Application.Invoke(delegate 
			{
				MessageDialog md = new MessageDialog(pWindow,
					  DialogFlags.Modal, MessageType.Warning,
					  ButtonsType.Ok, pValue);
				md.Run();
				md.Destroy();
			});
		}


		public static int ShowConfirm(Window pWindow, string pMessage)
		{
			int result = 0;

			Application.Invoke(delegate 
			{
				


				MessageDialog md = new MessageDialog(pWindow,
					DialogFlags.DestroyWithParent, MessageType.Question,
					ButtonsType.YesNo, pMessage);

				result = md.Run();
				md.Destroy();
			});

			return result;
		}


		public static void CloseApplication ()
		{
			Application.Quit();
		}


		public static void Focus (Widget pWidget)
		{
			pWidget.HasFocus = true;
		}


		public static string GetText(Label pLabel)
		{
			string result;


			//Application.Invoke (
			//	delegate
			//	{
					result = pLabel.Text;
			//	});

			return result;
		}


		public static string GetText(Entry pTextBox)
		{
			string result;


			//Application.Invoke (
			//	delegate
			//	{
			result = pTextBox.Text;
			//	});

			return result;
		}


		public static void ClearText(Label pLabel)
		{
			SetText (pLabel, string.Empty);
		}


		public static void ClearText(Entry pEntry)
		{
			SetText(pEntry, string.Empty);
		}


		public static void SetText(Label pLabel, string pText)
		{
			Application.Invoke(delegate { pLabel.Text = pText; });
		}


		public static void SetText(Entry pTextBox, string pText)
		{
			Application.Invoke(delegate { pTextBox.Text = pText; });
		}


		public static void ClearTitle(Window pControl)
		{
			SetTitle (pControl, string.Empty);
		}
		
		
		public static void SetTitle (Window pControl, string pText)
		{
			Application.Invoke(delegate { pControl.Title = pText; });
		}


		public static void ClearFraction(ProgressBar pProgresBar)
		{
			SetFraction (pProgresBar, 0);
		}


		public static void SetFraction(ProgressBar pProgresBar, double pPercentage100)
		{
			Application.Invoke(delegate { pProgresBar.Fraction = pPercentage100 / 100.0; });
		}


		public static void SetAdvance(Label pLabel, ProgressBar pProgressBar, string pMessage = "", int pTotalWorkCount = 0, int pReadyWorkCount = 0, int pTotalErrors = 0, long pItemElapsedMs = 0)
		{
			double advance100;
			
			
			advance100 = ProcessHelper.GetAdvancePercentage100 (pTotalWorkCount, pReadyWorkCount + pTotalErrors);

			SetText (pLabel, 
			         (string.IsNullOrEmpty (pMessage) ? string.Empty : pMessage + "... ") + 
			         string.Format ("Procesados {0} de {1}{3}: {2:0.##}%", pReadyWorkCount, pTotalWorkCount, advance100, 
			               (pTotalErrors > 0 ? string.Format (" con {0} errores", pTotalErrors) : string.Empty)));
			
			SetFraction (pProgressBar, advance100);
		}
		
		
		public static void ClearAdvance (Label pLabel, ProgressBar pProgressBar)
		{
			ClearText (pLabel);
			ClearFraction (pProgressBar);
		}

		#endregion
	}
}
